/**
 * evaluation
 *
 * @author LongShu 2017/06/09
 */
(function () {
    var evaluationApp = new Vue({
        el: '#evaluation',
        data: {
            selectSemester: '0',
            semesterList: [],
            evaluationList: [],
            evaluation: {
                fens: "5",
                pingyu: "一键好评!"
            },
            msg: null,
            showList: false,
            refreshing: false
        },
        methods: {
            chooseSemester: function () {
                if (this.selectSemester !== '0') {
                    this.showEvaluationList();
                }
            },
            showEvaluationList: function () {
                if (this.selectSemester === '0') {
                    this.showMsg("请选择学期!");
                    return;
                }
                if (this.refreshing) {
                    return;
                }
                this.refreshing = true;

                this.$http.get(fixUrl("evaluation/evaluationList/" + this.selectSemester)).then(function (response) {
                    $('.alert').hide();
                    var webData = new WebData(response.body);
                    if (webData.isError()) {
                        this.showMsg(webData.msg);
                        return false;
                    }

                    this.refreshing = false;
                    this.evaluationList = webData.data;

                    if (webData.data.length < 1) {
                        this.showMsg("哎呀,好像没有数据哎!");
                        this.showList = false;
                        return false;
                    }
                    this.showList = true;
                }, function (response) {
                    console.warn(response.body);
                    this.showMsg("获取评教老师失败!");
                });
            },
            submitEvaluation: function () {
                // 准备数据
                this.evaluation.jxbbh = "";
                this.evaluation.jsbh = "";
                var ea;
                for (var i in this.evaluationList) {
                    ea = this.evaluationList[i];
                    if (0 === ea.Cpzt) {// 未测评
                        this.evaluation.jxbbh += ea.Jxbbhs + ",";
                        this.evaluation.jsbh += ea.Jsbh + ",";
                    }
                }

                if (this.evaluation.jxbbh.length < 1) {
                    alert("好像已经评完了哦!");
                    return false;
                }
                // 清空缓存的key
                this.evaluation.semester = this.selectSemester;

                // 发送评教任务
                this.$http.post(fixUrl("evaluation/submitEvaluation"), this.evaluation).then(function (response) {
                    var webData = new WebData(response.body);
                    if (webData.isError()) {
                        this.showMsg(webData.msg);
                        return false;
                    }
                    // 根据评教数量来延时刷新
                    var count = webData.data;
                    var time = (count * 6) + 1;// 6秒评完已经是很理想的情况了
                    if (!evaluationApp.refreshing) {
                        setTimeout(refreshEvaluationList, 1000 * time);// 自动刷新
                    }
                    alert(webData.msg + " 将在" + time + "秒后完成评教.");
                }, function (response) {
                    console.warn(response.body);
                    this.showMsg("评教老师们失败!");
                });
            },
            showMsg: function (msg) {
                this.msg = msg;
                $('.alert').show();
            }
        }
    });

    // setTimeout使用
    window.refreshEvaluationList = function () {
        evaluationApp.showEvaluationList();
    };

    Vue.http.get(fixUrl("commonData/semesterList?limit=8")).then(function (response) {
        var webData = new WebData(response.body);
        if (webData.isError()) {
            this.showMsg(webData.msg);
            return;
        }
        evaluationApp.semesterList = webData.data;
    }, function (response) {
        console.warn(response.body);
        this.showMsg("获取学期列表失败!");
    });
})();
